package com.example.easybuy.controller;

import com.example.easybuy.service.EmailService;
import com.example.easybuy.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("email")
public class EmailController {
    @Autowired
    private EmailService emailService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @RequestMapping("/proveEmail")
    public AjaxResult proveEmail(String email) {
        String proveCode = (int) (Math.random() * 10000) + "";
        if (proveCode.length()<4){
            emailService.sendSimpleMail(email, proveCode+0);
        }else {
            emailService.sendSimpleMail(email, proveCode);
        }
        stringRedisTemplate.opsForValue().set(email, proveCode, 1, TimeUnit.MINUTES);
        return AjaxResult.success().add("proveCode", proveCode);
    }

    @RequestMapping("/getProCode")
    public AjaxResult getProCode(String email) {
        String proveCode = stringRedisTemplate.opsForValue().get(email);
        return AjaxResult.success().add("proveCode", proveCode);
    }
}